//package com.yunxin.core.application.service.impl;
//
//import cn.hutool.core.text.CharSequenceUtil;
//import com.yunxin.common.exception.BusinessException;
//import com.yunxin.common.result.ResultCode;
//import com.yunxin.common.utils.SnowflakeKeyGenerator;
//import com.yunxin.core.application.assembler.ProductAssembler;
//import com.yunxin.core.application.command.product.*;
//import com.yunxin.core.application.service.ProductApplicationAdminService;
//import com.yunxin.core.domain.model.merchant.MerchantInfo;
//import com.yunxin.core.domain.model.product.*;
//import com.yunxin.core.domain.service.merchant.MerchantService;
//import com.yunxin.core.domain.service.product.ProductService;
//import com.yunxin.core.infra.common.product.enums.ProductAbutmentTypeEnum;
//import com.yunxin.core.infra.common.product.enums.ProductStatusEnum;
//import com.yunxin.core.utils.infra.common.constant.CoreResultCode;
//import com.yunxin.core.utils.infra.common.enums.KeyGeneratorPrefixEnum;
//import com.yunxin.statistics.domain.external.StatisticsExternalService;
//import lombok.RequiredArgsConstructor;
//import org.springframework.stereotype.Service;
//
//import java.util.ArrayList;
//import java.util.Date;
//import java.util.List;
//import java.util.Objects;
//
///**
// * @author zero°
// * @since 2023-05-31
// */
//@Service
//@RequiredArgsConstructor
//public class ProductApplicationAdminServiceImpl implements ProductApplicationAdminService {
//
//    private final ProductInfoRepository productInfoRepository;
//
//    private final ProductCustomConfigRepository productCustomConfigRepository;
//
//    private final ProductThrowInConfigRepository productThrowInConfigRepository;
//
//    private final ProductUnitPriceConfigRepository productUnitPriceConfigRepository;
//
//    private final ProductUnitPriceConfigVersionRepository productUnitPriceConfigVersionRepository;
//
//    private final ProductAdvanceChargeRecordRepository productAdvanceChargeRecordRepository;
//
//    private final ProductService productService;
//
//    private final StatisticsExternalService statisticsExternalService;
//
//    private final ProductAssembler productAssembler;
//
//    private final SnowflakeKeyGenerator sequenceGenerator;
//
//    private final MerchantService merchantService;
//
//    @Override
//    public Long addProductPromotionInfo(AddProductPromotionInfoCommand command) {
//        Long id = productInfoRepository.getProductIdByMerchantIdAndProductName(command.getMerchantId(), command.getProductName());
//        if (Objects.nonNull(id)) {
//            throw new BusinessException(CoreResultCode.PRODUCT_NAME_EXIST);
//        }
//        ProductInfo productInfo = productAssembler.fromAddProductPromotionInfoCommand(command);
//        productInfo.setProductCode(sequenceGenerator.generateKey(KeyGeneratorPrefixEnum.PRODUCT.getPrefix()));
//        productInfo.setEditor(command.getCreator());
//        ProductPromotionInfo productPromotionInfo = productAssembler.fromAddProductPromotionInfoCommandForPromotionInfo(command);
//        productPromotionInfo.setEditor(command.getCreator());
//        return productService.addProductPromotion(productInfo, productPromotionInfo);
//    }
//
//    @Override
//    public boolean updateProductPromotionInfo(UpdateProductPromotionInfoCommand command) {
//        ProductInfo oldProductInfo = productInfoRepository.getProductInfoById(command.getProductId());
//        if (Objects.isNull(oldProductInfo)) {
//            throw new BusinessException(ResultCode.DATA_NO_EXIST_ERROR);
//        }
//        Long id = productInfoRepository.getProductIdByMerchantIdAndProductName(command.getMerchantId(), command.getProductName());
//        if (Objects.nonNull(id) && !oldProductInfo.getId().equals(id)) {
//            throw new BusinessException(CoreResultCode.PRODUCT_NAME_EXIST);
//        }
//        ProductInfo productInfo = productAssembler.fromUpdateProductPromotionInfoCommand(command);
//        productInfo.setVersion(oldProductInfo.getVersion() + 1);
//        ProductPromotionInfo productPromotionInfo = productAssembler.fromUpdateProductPromotionInfoCommandForPromotionInfo(command);
//        productPromotionInfo.setVersion(productInfo.getVersion());
//        return productService.updateProductPromotion(productInfo, productPromotionInfo);
//    }
//
//    @Override
//    public boolean operateProductPromotionCustomConfig(ProductCustomConfigOperateCommand command) {
//        ProductInfo productInfo = productInfoRepository.getProductInfoById(command.getProductId());
//        if (Objects.isNull(productInfo)) {
//            throw new BusinessException(CoreResultCode.PRODUCT_NOT_EXIST);
//        }
//        ProductCustomConfig oldCustomConfig = productCustomConfigRepository.getProductCustomConfigByProductId(command.getProductId());
//        boolean result;
//        if (Objects.isNull(oldCustomConfig)) {
//            oldCustomConfig = productAssembler.fromProductCustomConfigOperateCommand(command);
//            oldCustomConfig.setCreator(command.getOperator());
//            oldCustomConfig.setEditor(command.getOperator());
//            result = productCustomConfigRepository.saveProductCustomConfig(oldCustomConfig);
//        } else {
//            ProductCustomConfig updateCustomConfig = productAssembler.fromProductCustomConfigOperateCommand(command);
//            updateCustomConfig.setId(oldCustomConfig.getId());
//            updateCustomConfig.setEditor(command.getOperator());
//            updateCustomConfig.setVersion(oldCustomConfig.getVersion() + 1);
//            result = productCustomConfigRepository.updateProductCustomConfig(updateCustomConfig);
//        }
//        return result;
//    }
//
//    @Override
//    public boolean operateProductPromotionThrowInConfig(ProductThrowInConfigOperateCommand command) {
//        if (ProductAbutmentTypeEnum.H5.getValue().equals(command.getAbutmentType()) && CharSequenceUtil.isBlank(command.getJumpUrl())) {
//            throw new BusinessException(CoreResultCode.PRODUCT_JUMP_URL_BLANK);
//        }
//        if (ProductAbutmentTypeEnum.H5.getValue().equals(command.getAbutmentType())
//            && command.getIsLogin()==1 && CharSequenceUtil.isBlank(command.getUserProtocol())) {
//            throw new BusinessException(CoreResultCode.PRODUCT_UV_LOGIN_JUMP_URL_BLANK);
//        }
//        if (ProductAbutmentTypeEnum.ENTERPRISE_WECHAT.getValue().equals(command.getAbutmentType()) && CharSequenceUtil.isBlank(command.getJumpUrl())) {
//            throw new BusinessException(CoreResultCode.PRODUCT_WECHAT_JUMP_URL_BLANK);
//        }
//        if (ProductAbutmentTypeEnum.WECHAT_OFFICIAL_ACCOUNT.getValue().equals(command.getAbutmentType()) && CharSequenceUtil.isBlank(command.getJumpUrl())) {
//            throw new BusinessException(CoreResultCode.PRODUCT_WECHAT_OFFICIAL_ACCOUNT_JUMP_URL_BLANK);
//        }
//        ProductInfo productInfo = productInfoRepository.getProductInfoById(command.getProductId());
//        if (Objects.isNull(productInfo)) {
//            throw new BusinessException(CoreResultCode.PRODUCT_NOT_EXIST);
//        }
//        ProductThrowInConfig oldThrowInConfig = productThrowInConfigRepository.getProductThrowInConfigByProductId(command.getProductId());
//        boolean result;
//        if (Objects.isNull(oldThrowInConfig)) {
//            oldThrowInConfig = productAssembler.fromProductThrowInConfigOperateCommand(command);
//            oldThrowInConfig.setCreator(command.getOperator());
//            oldThrowInConfig.setEditor(command.getOperator());
//            oldThrowInConfig.setBeProductId(command.getBeProductId());
//            oldThrowInConfig.setIsLogin(command.getIsLogin());
//            oldThrowInConfig.setUserProtocol(command.getUserProtocol());
//            result = productThrowInConfigRepository.saveProductThrowInConfig(oldThrowInConfig);
//        } else {
//            ProductThrowInConfig updateThrowInConfig = productAssembler.fromProductThrowInConfigOperateCommand(command);
//            updateThrowInConfig.setId(oldThrowInConfig.getId());
//            updateThrowInConfig.setEditor(command.getOperator());
//            updateThrowInConfig.setVersion(oldThrowInConfig.getVersion() + 1);
//            updateThrowInConfig.setBeProductId(command.getBeProductId());
//            updateThrowInConfig.setIsLogin(command.getIsLogin());
//            updateThrowInConfig.setUserProtocol(command.getUserProtocol());
//            result = productThrowInConfigRepository.updateProductThrowInConfig(updateThrowInConfig);
//        }
//        return result;
//    }
//
//    @Override
//    public boolean operateProductPromotionUnitPriceConfig(ProductUnitPriceConfigOperateCommand command) {
//        ProductInfo productInfo = productInfoRepository.getProductInfoById(command.getProductId());
//        if (Objects.isNull(productInfo)) {
//            throw new BusinessException(CoreResultCode.PRODUCT_NOT_EXIST);
//        }
//        if (ProductStatusEnum.GROUNDING.getValue().equals(productInfo.getProductStatus())) {
//            throw new BusinessException(CoreResultCode.PRODUCT_STATUS_ERROR);
//        }
//        ProductUnitPriceConfig oldUnitPriceConfig = productUnitPriceConfigRepository.getProductUnitPriceConfigByProductId(command.getProductId());
//        boolean result;
//        if (Objects.isNull(oldUnitPriceConfig)) {
//            oldUnitPriceConfig = productAssembler.fromProductUnitPriceConfigOperateCommand(command);
//            oldUnitPriceConfig.setCreator(command.getOperator());
//            oldUnitPriceConfig.setEditor(command.getOperator());
//            result = productUnitPriceConfigRepository.saveProductUnitPriceConfig(oldUnitPriceConfig);
//            productUnitPriceConfigVersionRepository.saveProductUnitPriceConfigVersion(converterProductUnitPriceConfigVersion(oldUnitPriceConfig));
//        } else {
//            ProductUnitPriceConfig updateUnitPriceConfig = productAssembler.fromProductUnitPriceConfigOperateCommand(command);
//            updateUnitPriceConfig.setId(oldUnitPriceConfig.getId());
//            updateUnitPriceConfig.setEditor(command.getOperator());
//            updateUnitPriceConfig.setVersion(oldUnitPriceConfig.getVersion() + 1);
//            updateUnitPriceConfig.setCurrentVersion(oldUnitPriceConfig.getCurrentVersion() + 1);
//            result = productUnitPriceConfigRepository.updateProductUnitPriceConfig(updateUnitPriceConfig);
//            productUnitPriceConfigVersionRepository.saveProductUnitPriceConfigVersion(converterProductUnitPriceConfigVersion(updateUnitPriceConfig));
//        }
//        // 价格不一致时通知产品统计变更结算价格
//        if (oldUnitPriceConfig.getChargedPrice().compareTo(command.getChargedPrice()) != 0) {
//            statisticsExternalService.updateProductStatisticsSettlementPrice(command.getProductId(), new Date(), command.getChargedPrice());
//        }
//        return result;
//    }
//
//    @Override
//    public boolean updateProductStatus(UpdateProductStatusCommand command) {
//        ProductInfo productInfo = productInfoRepository.getProductInfoById(command.getId());
//        if (Objects.isNull(productInfo)) {
//            throw new BusinessException(ResultCode.DATA_NO_EXIST_ERROR);
//        }
//        checkBeProductStatus(command.getId(), command.getProductStatus());
//        if (ProductStatusEnum.GROUNDING.getValue().equals(command.getProductStatus())) {
//            boolean r = productInfoRepository.getProductSaveResult(command.getId());
//            if (r) {
//                throw new BusinessException(CoreResultCode.PRODUCT_UPDATE_STATUS_ERROR);
//            }
//            MerchantInfo merchantInfo = merchantService.selectByMerchantId(productInfo.getMerchantId());
//            if (Objects.isNull(merchantInfo) || merchantInfo.getMerchantStatus() == 1) {
//                throw new BusinessException(CoreResultCode.MERCHANT_IS_LOSE_EFFICACY);
//            }
//        }
//        ProductInfo updateProductInfoDO = new ProductInfo();
//        updateProductInfoDO.setId(command.getId());
//        updateProductInfoDO.setProductStatus(command.getProductStatus());
//        updateProductInfoDO.setEditor(command.getEditor());
//        return productInfoRepository.updateProductInfo(updateProductInfoDO);
//    }
//
//    @Override
//    public boolean addProductAdvanceChargeRecord(AddProductAdvanceChargeRecordCommand command) {
//        ProductAdvanceChargeRecord productAdvanceChargeRecord = productAssembler.fromAddProductAdvanceChargeRecordCommand(command);
//        return productAdvanceChargeRecordRepository.saveProductAdvanceChargeRecord(productAdvanceChargeRecord);
//    }
//
//    @Override
//    public boolean updateProductAdvanceChargeRecord(UpdateProductAdvanceChargeRecordCommand command) {
//        ProductAdvanceChargeRecord oldProductAdvanceChargeRecord = productAdvanceChargeRecordRepository.getProductAdvanceChargeRecordById(command.getId());
//        if (Objects.isNull(oldProductAdvanceChargeRecord)) {
//            throw new BusinessException(ResultCode.DATA_NO_EXIST_ERROR);
//        }
//        ProductAdvanceChargeRecord productAdvanceChargeRecord = productAssembler.fromUpdateProductAdvanceChargeRecordCommand(command);
//        productAdvanceChargeRecord.setVersion(oldProductAdvanceChargeRecord.getVersion() + 1);
//        return productAdvanceChargeRecordRepository.updateProductAdvanceChargeRecord(productAdvanceChargeRecord);
//    }
//
////    @Override
////    public Boolean updateProductProhibitStatus(UpdateProductProhibitStatusCommand command) {
////        command.getIds().forEach(id->{
////            ProductInfo productInfo = productInfoRepository.getProductInfoById(id);
////            if (Objects.isNull(productInfo)) {
////                throw new BusinessException(ResultCode.DATA_NO_EXIST_ERROR);
////            }
////            if (ProductStatusEnum.GROUNDING.getValue().equals(command.getProhibitStatus())) {
////                boolean r = productInfoRepository.getProductSaveResult(id);
////                if (r) {
////                    throw new BusinessException(CoreResultCode.PRODUCT_UPDATE_STATUS_ERROR);
////                }
////            }
////            ProductInfo updateProductInfoDO = new ProductInfo();
////            updateProductInfoDO.setId(id);
////            updateProductInfoDO.setEditor(command.getEditor());
////            try {
////                productInfoRepository.updateProductProhibitStatus(updateProductInfoDO);
////            } catch (Exception e) {
////                throw new RuntimeException("更新产品状态异常");
////            }
////        });
////
////        return true;
////    }
//
//    /**
//     * ProductUnitPriceConfig 转换 ProductUnitPriceConfigVersion
//     *
//     * @param productUnitPriceConfig productUnitPriceConfig
//     * @return ProductUnitPriceConfigVersion
//     */
//    private ProductUnitPriceConfigVersion converterProductUnitPriceConfigVersion(ProductUnitPriceConfig productUnitPriceConfig) {
//        ProductUnitPriceConfigVersion productUnitPriceConfigVersion = new ProductUnitPriceConfigVersion();
//        productUnitPriceConfigVersion.setProductId(productUnitPriceConfig.getProductId());
//        productUnitPriceConfigVersion.setTakeEffectTime(productUnitPriceConfig.getTakeEffectTime());
//        productUnitPriceConfigVersion.setChargedType(productUnitPriceConfig.getChargedType());
//        productUnitPriceConfigVersion.setChargedPrice(productUnitPriceConfig.getChargedPrice());
//        productUnitPriceConfigVersion.setChargedWay(productUnitPriceConfig.getChargedWay());
//        productUnitPriceConfigVersion.setCurrentVersion(productUnitPriceConfig.getCurrentVersion());
//        productUnitPriceConfigVersion.setCreator(productUnitPriceConfig.getEditor());
//        return productUnitPriceConfigVersion;
//    }
//
//    private void checkBeProductStatus(Long productId, Integer productStatus) {
//        ProductThrowInConfig productThrowInfo = productThrowInConfigRepository.getProductThrowInConfigByProductId(productId);
//        if (productThrowInfo != null) {
//            if (productThrowInfo.getAbutmentType() == 4) { // 是企微的产品 上架必须和前置产品同步
//                Long beProductId = productThrowInfo.getBeProductId();
//                if (beProductId == null) {
//                    throw new BusinessException("请配置前置产品");
//                }
//                ProductInfo productInfo = productInfoRepository.getProductInfoById(beProductId);
//                if (!Objects.isNull(productInfo)) {
//                    if (ProductStatusEnum.GROUNDING.getValue().equals(productStatus)) { // 上架操作
//                        // 判断该产品的前置产品是否下架
//                        if (ProductStatusEnum.OFF_SHELF.getValue().equals(productInfo.getProductStatus())) {
//                            throw new BusinessException("该产品的前置产品:" + productInfo.getProductName() + "需同步上架");
//                        }
//                    }
//                }
//            } else { //不是企微的产品 下架时必须,同时下架前置产品
//                List<ProductThrowInConfig> beConfigInfo = productThrowInConfigRepository.getProductThrowInConfigByBeProductId(productId);
//                ArrayList<ProductInfo> failList = new ArrayList<>();
//                if (!beConfigInfo.isEmpty()) { // 存在前置产品
//                    beConfigInfo.forEach(info -> {
//                        Long beProductId = info.getProductId();
//                        ProductInfo BeProductInfo = productInfoRepository.getProductInfoById(beProductId);
//                        if (BeProductInfo != null) {
//                            if (ProductStatusEnum.OFF_SHELF.getValue().equals(productStatus)) { // 下架操作
//                                // 判断该产品的前置产品是否上架
//                                if (ProductStatusEnum.GROUNDING.getValue().equals(BeProductInfo.getProductStatus())) {
//                                    failList.add(BeProductInfo);
//                                }
//                            }
//                        }
//                    });
//                }
//                if (!failList.isEmpty()) {
//                    throw new BusinessException("该产品的前置产品:" + failList.stream().map(ProductInfo::getProductName).toList() + "需同步下架");
//                }
//
//            }
//        }
//    }
//}
